//
//  LZFinancialRecordsViewController.m
//  laziz_Merchant
//
//  Created by xbnzd on 16/12/17.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZFinancialRecordsViewController.h"
#import "LZFinanicalOneMonthRecordsViewController.h"
#import "LZFinancialRecordsCell.h"
#import "MJRefresh.h"
#import "LZFinancialPresenter.h"
#import "LZFinancialPayedModel.h"
#import "LZUserModel.h"
#import "UITableView+LZTableView.h"

@interface LZFinancialRecordsViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIView *topTitleView;
@property (weak, nonatomic) IBOutlet UILabel *serialNumberLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *serialNumberLabelWidth;
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *timeLabelWidth;
@property (weak, nonatomic) IBOutlet UILabel *amountLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *amountLabelWidth;

@property (assign, nonatomic) NSInteger currentPage;
@property (assign, nonatomic) NSTimeInterval timestamp;

@property (strong, nonatomic) LZFinancialPresenter *financialPresenter;
@property (strong, nonatomic) NSMutableArray *dataArr;

@end

@implementation LZFinancialRecordsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.umLogPageViewName = @"已经结款记录页面";
    _financialPresenter = [[LZFinancialPresenter alloc] init];
    _dataArr = @[].mutableCopy;
    self.title = LZLocalizedString(@"home_menu_financial_records", nil);
    _currentPage = 1;
    _timestamp = 0;
        
    [self configureTopTitleView];
    
    [self configureTableView];
    
    [self getListDataWithPage:_currentPage];
}

#pragma mark -
#pragma mark private
- (void)upLoadMoreVideoListData
{
    [self getListDataWithPage:self.currentPage];
}

- (void)getListDataWithPage:(NSInteger)cPage
{
    WS(wSelf);
    [self.financialPresenter getFinanicalHaveBeenPayedListWithSellerID:[LZUserModel Instance].sellerId CurrentPage:cPage Limit:10 resultSuccess:^(NSArray *listArr, NSInteger totalPage, NSTimeInterval timeStamp) {
        
        if (totalPage > cPage) {
            [wSelf.tableView.mj_footer endRefreshing];
        }else{
            [wSelf.tableView setNoMoreDataFooterText];
            [wSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        if (listArr.count > 0) {
            [wSelf.dataArr addObjectsFromArray:listArr];
            [wSelf.tableView reloadData];
        }
        wSelf.currentPage ++;
        
    } Failure:^(NSString *fail) {
        
    }];
}

#pragma mark -
#pragma mark UI
- (void)configureTopTitleView
{
    GET_SCREEN_WIDTH(cellWidth);
    _serialNumberLabel.text = LZLocalizedString(@"serial_number", nil);
    _serialNumberLabelWidth.constant = (cellWidth - 92)*0.4;
    
    _timeLabel.text = LZLocalizedString(@"time", nil);
    _timeLabelWidth.constant = (cellWidth - 92)*0.3;
    
    _amountLabel.text = LZLocalizedString(@"amount", nil);
    _amountLabelWidth.constant = (cellWidth - 92)*0.3;
}

- (void)configureTableView
{
    [self.tableView registerNib:[UINib nibWithNibName:@"LZFinancialRecordsCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableView.tableHeaderView = self.topTitleView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upLoadMoreVideoListData)];
    [self.tableView configFooterRefreshText];
}

#pragma mark -
#pragma mark tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LZFinancialRecordsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    LZFinancialPayedModel *m = self.dataArr[indexPath.row];
    cell.fModel = m;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LZFinancialPayedModel *m = self.dataArr[indexPath.row];
    LZFinanicalOneMonthRecordsViewController *vc = [[LZFinanicalOneMonthRecordsViewController alloc] init];
    vc.payedModel = m;
    [self.navigationController pushViewController:vc animated:YES];
    
}




@end
